package com.atguigu.serviceedu.service.impl;

import com.alibaba.excel.EasyExcel;
import com.atguigu.serviceedu.entity.EduSubject;
import com.atguigu.serviceedu.entity.excel.SubjectData;
import com.atguigu.serviceedu.entity.subject.oneSubject;
import com.atguigu.serviceedu.entity.subject.twoSubject;
import com.atguigu.serviceedu.listener.SubjectExcelListener;
import com.atguigu.serviceedu.mapper.EduSubjectMapper;
import com.atguigu.serviceedu.service.EduSubjectService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 课程科目 服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@Service
public class EduSubjectServiceImpl extends ServiceImpl<EduSubjectMapper, EduSubject> implements EduSubjectService {

    //添加课程分类
    @Override
    public void saveSubject(MultipartFile file,EduSubjectService subjectService) {
        try{
            //获取上传的文件流
            InputStream in = file.getInputStream();
            EasyExcel.read(in,SubjectData.class,new SubjectExcelListener(subjectService)).sheet().doRead();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    //树形分类
    @Override
    public List<oneSubject> getAllSubject() {
        QueryWrapper<EduSubject> wrapperone = new QueryWrapper<>();

        //查所有的一级分类
        wrapperone.eq("parent_id","0");
        List<EduSubject> oneSubjectList = baseMapper.selectList(wrapperone);

        //查所有的二级分类
        QueryWrapper<EduSubject> wrappertwo = new QueryWrapper<>();
        wrappertwo.ne("parent_id","0");
        List<EduSubject> twoSubjectList = baseMapper.selectList(wrappertwo);

        //所有数据封装
        List<oneSubject> list = new ArrayList<>();

        //封装一级分类
        for (int i = 0; i <oneSubjectList.size(); i++) {
            //数据查出的数据
            EduSubject eduSubject = oneSubjectList.get(i);
            //放入到list里面
            oneSubject oneSubject = new oneSubject();

            //如果属性名相同可以通过spring提供的工具类实现
            BeanUtils.copyProperties(eduSubject,oneSubject);
//            oneSubject.setId(eduSubject.getId());
//            oneSubject.setTitle(eduSubject.getTitle());

            list.add(oneSubject);
            List<twoSubject> tList = new ArrayList<>();
            for (int j = 0; j < twoSubjectList.size(); j++) {
                //数据库查询的对象
                EduSubject tsubject = twoSubjectList.get(j);

                if(eduSubject.getId().equals(tsubject.getParentId())){
                    twoSubject twoSubject = new twoSubject();//封装为这个对象
                    BeanUtils.copyProperties(tsubject,twoSubject);
                    tList.add(twoSubject);
                }
            }

            oneSubject.setChildren(tList);
        }
        //封装二级分类
        return list;
    }

}
